﻿using System;
using System.Drawing;
using LinkDownloader.Forms.UserControls;
using LinkDownloader.Core;

namespace LinkDownloader.DownloadManger
{

    /// <summary>
    /// The Utils class that holds all the static global utility properties and methods.
    /// </summary>
    internal class Utils
    {

        private static int SizeGB = 1024 * 1024;

        private static Utility utility = null;

        /// <summary>
        /// Initializes the static class. Can be called during splash screen display.
        /// </summary>
        public static void Initialize()
        {

            utility = new Utility();

        }

        /// <summary>
        /// Formats the specified size into user readable text with units.
        /// </summary>
        /// <param name="size">The size to be formatted, should be in Kilo Bytes.</param>
        /// <returns>A string with units; KB, MB or GB.</returns>
        public static string FormatSize(long size)
        {
            if (size > 1024)
            {
                if (size > SizeGB)
                {
                    return string.Format("{0:0.00} GB", (double)size / SizeGB);
                }
                else
                {
                    return string.Format("{0:0.00} MB", (double)size / 1024.0);
                }
            }
            return string.Format("{0} KB", size);
        }

        /// <summary>
        /// Formats the specified status into user readable text.
        /// </summary>
        /// <param name="status">The status value to be formatted.</param>
        /// <returns>A string of formatted status.</returns>
        public static string FormatStatus(DownloadStatus status)
        {

            switch (status)
            {

                case DownloadStatus.Authenticate:
                    return "Waiting for authentication";

                case DownloadStatus.Cancelled:
                    return "Cancelled";

                case DownloadStatus.Completed:
                    return "Completed";

                case DownloadStatus.Connecting:
                    return "Connecting...";

                case DownloadStatus.Downloading:
                    return "Downloading...";

                case DownloadStatus.Error:
                    return "Error encountered...";

                case DownloadStatus.Paused:
                    return "Paused";

                case DownloadStatus.Redirecting:
                    return "Redirecting...";

                case DownloadStatus.Skipped:
                    return "Skipped";

                case DownloadStatus.None:
                    return "Not started";

                default:
                    return "Unknown";

            }

        }

        /// <summary>
        /// Formats the specified time to user readable text.
        /// </summary>
        /// <param name="time">The time to be formatted.</param>
        /// <returns>The formatted time containing at most the two closest units ("days and hours", "hours and mins", etc).</returns>
        public static string FormatTime(TimeSpan time)
        {
            if (time.Days > 0) { return string.Format("{0} days, {1} hours", time.Days, time.Hours); }
            if (time.Hours > 0) { return string.Format("{0} hours, {1} mins", time.Hours, time.Minutes); }
            if (time.Minutes > 0) { return string.Format("{0} mins, {1} secs", time.Minutes, time.Seconds); }
            return string.Format("Approx. {0} secs", time.Seconds);
        }

        /// <summary>
        /// Gets the Icon of the File Type depending on the provided extension.
        /// </summary>
        /// <param name="extension">The associated file extension.</param>
        /// <returns>An Image that corresponds to the file extension. An unknow file type would return the default Image.</returns>
        public static Image GetFileIcon(string extension)
        {

            extension = extension.ToLower();

            if (utility.FileIconList.Images.ContainsKey(extension)) { return utility.FileIconList.Images[extension]; }

            foreach (string name in utility.FileIconNames)
            {
                if (extension.Contains(name) || name.Contains(extension)) { return utility.FileIconList.Images[name]; }
            }

            switch (extension)
            {
                case "jpeg":
                    return utility.FileIconList.Images["jpg"];

                case "mpg":
                    return utility.FileIconList.Images["mpeg"];

                case "mp4":
                    return utility.FileIconList.Images["vid"];

                default:
                    return utility.FileIconList.Images["default"];
            }

        }

    }
         
}
